//
//  WBLottieAnimationView.m
//  JMWManualPackageLib
//
//  Created by WENBO on 2020/4/14.
//  Copyright © 2020 wenbo. All rights reserved.
//

#import "WBLottieAnimationView.h"
#import "Masonry.h"

CGSize const WBLottieAnimationSize = {60, 60};
NSString *const WBDefaultLottieAnimationFileName = @"jmw_login_lottie";

@interface WBLottieAnimationView ()

@end

@implementation WBLottieAnimationView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

+ (WBLottieAnimationView *)wb_showAnimationView {
    return [self wb_showAnimationViewToView:nil
                                   fileName:nil
                                        url:nil
                                    offsetY:0
                                 topPadding:0];
}

+ (WBLottieAnimationView *)wb_showAnimationViewToView:(nullable UIView *)view {
    return [self wb_showAnimationViewToView:view
                                   fileName:nil
                                        url:nil
                                    offsetY:0
                                 topPadding:0];
}

+ (WBLottieAnimationView *)wb_showAnimationViewToView:(UIView *)view
                                             fileName:(NSString *)fileName
                                              offsetY:(CGFloat)offsetY
                                           topPadding:(CGFloat)topPadding {
    return [self wb_showAnimationViewToView:view
                                   fileName:fileName
                                        url:nil
                                    offsetY:offsetY
                                 topPadding:topPadding];
}


+ (WBLottieAnimationView *)wb_showAnimationViewToView:(nullable UIView *)view
                                              offsetY:(CGFloat)offsetY
                                           topPadding:(CGFloat)topPadding {
    return [self wb_showAnimationViewToView:view
                                   fileName:nil
                                        url:nil
                                    offsetY:offsetY
                                 topPadding:topPadding];
}

+ (WBLottieAnimationView *)wb_showAnimationViewToView:(UIView *)view
                                                  url:(nullable NSString *)url
                                              offsetY:(CGFloat)offsetY
                                           topPadding:(CGFloat)topPadding {
    return [self wb_showAnimationViewToView:view
                                   fileName:nil
                                        url:nil
                                    offsetY:offsetY
                                 topPadding:topPadding];
}


+ (WBLottieAnimationView *)wb_showAnimationViewToView:(UIView *)view
                                             fileName:(NSString *)fileName
                                                  url:(NSString *)url
                                              offsetY:(CGFloat)offsetY
                                           topPadding:(CGFloat)topPadding {
    WBLottieAnimationView *animationView = [WBLottieAnimationView new];
    
    /// 默认加载动画
    if (!fileName && !url) {
        if (!animationView.lottieAnimationView) {
            LOTAnimationView *lottieAnimationView = [LOTAnimationView animationNamed:WBDefaultLottieAnimationFileName];
            animationView.lottieAnimationView = lottieAnimationView;
            lottieAnimationView.contentMode = UIViewContentModeScaleAspectFit;
            lottieAnimationView.loopAnimation = YES;
            animationView.lottieAnimationView = lottieAnimationView;
        }
    }
    
    /// 本地json文件
    if (fileName.length > 0) {
        if (!animationView.lottieAnimationView) {
            LOTAnimationView *lottieAnimationView = [LOTAnimationView animationNamed:fileName];
            animationView.lottieAnimationView = lottieAnimationView;
            lottieAnimationView.contentMode = UIViewContentModeScaleAspectFit;
            lottieAnimationView.loopAnimation = YES;
            animationView.lottieAnimationView = lottieAnimationView;
        }
    }
    
    /// 远程地址
    if (url.length > 0) {
        if (!animationView.lottieAnimationView) {
            LOTAnimationView *lottieAnimationView = [[LOTAnimationView alloc]initWithContentsOfURL:[NSURL URLWithString:url]];
            animationView.lottieAnimationView = lottieAnimationView;
            lottieAnimationView.contentMode = UIViewContentModeScaleAspectFit;
            lottieAnimationView.loopAnimation = YES;
            animationView.lottieAnimationView = lottieAnimationView;
        }
    }
    
    /// start animation
    [animationView.lottieAnimationView playWithCompletion:^(BOOL animationFinished) {
        
    }];
    
    if (!view) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    
    /// animationView layout
    [view addSubview:animationView];
    [animationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.bottom.equalTo(view);
        make.top.mas_equalTo(topPadding);
    }];
    
    /// lottieAnimationView layout
    [animationView addSubview:animationView.lottieAnimationView];
    [animationView.lottieAnimationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(animationView);
        make.centerY.equalTo(animationView).offset(offsetY);
        make.size.mas_equalTo(WBLottieAnimationSize);
    }];
    
    return animationView;
}

+ (void)wb_removeAnimationView {
    [self wb_removeAnimationForView:nil];
}

+ (void)wb_removeAnimationForView:(nullable UIView *)view {
    if (!view) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    
    NSArray *lottties = [self allLottieAnimationForView:view];
    for (WBLottieAnimationView *lottie in lottties) {
        [lottie done];
    }
}

- (void)done {
    self.alpha = 0.f;
    [self.lottieAnimationView removeFromSuperview];
    self.lottieAnimationView = nil;
    [self removeFromSuperview];
}

// MARK: - Private Method
+ (NSArray *)allLottieAnimationForView:(UIView *)view {
    NSMutableArray *huds = [NSMutableArray array];
    NSArray *subviews = view.subviews;
    for (UIView *aView in subviews) {
        if ([aView isKindOfClass:self]) {
            [huds addObject:aView];
        }
    }
    return [NSArray arrayWithArray:huds];
}

// MARK: - setter
- (void)setAnimationSize:(CGSize)animationSize {
    _animationSize = animationSize;
    [self.lottieAnimationView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(animationSize);
    }];
}

@end
